#include <iostream>
#include <typeinfo>
#include <QtGui>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "protoolsstandard.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // Setting a new protools object up.
    ourProtoolsObject = new ProtoolsStandard;

    this->ClearOutTheScreen();
    this->SetupConcertTabs();
    ui->tabWidget->setCurrentIndex(0);
    this->UpdateProtoolsObject();
    this->HasChanged = false;
    this->fileString = tr("");
    this->IsDeletingTickets = false;
    this->IsDeletingArtists = false;
    this->IsDeletingVenues = false;
    this->IsDeletingDates = false;
    this->IsDeletingTaxes = false;

    this->IsSingleFile = false;

    // This is to show the database login if there is one.
    dbl = new CSDatabaseLogin;
    connect(dbl, SIGNAL(LoggedIn()), this, SLOT(on_LoggedIn()));
    connect(dbl, SIGNAL(NotLoggedIn()), this, SLOT(on_NotLoggedIn()));
    connect(dbl, SIGNAL(Canceled()), this, SLOT(on_Canceled()));
    connect(dbl, SIGNAL(SingleFile()), this, SLOT(on_SingleFile()));
    dbl->exec();
}

MainWindow::~MainWindow()
{
    delete ourProtoolsObject;
    delete ui;
}

// This is for the database login

void MainWindow::DisconnectLoginDialog()
{
    disconnect(dbl, SIGNAL(LoggedIn()), this, SLOT(on_LoggedIn()));
    disconnect(dbl, SIGNAL(NotLoggedIn()), this, SLOT(on_NotLoggedIn()));
    disconnect(dbl, SIGNAL(Canceled()), this, SLOT(on_Canceled()));
    disconnect(dbl, SIGNAL(SingleFile()), this, SLOT(on_SingleFile()));
}

void MainWindow::on_LoggedIn()
{
    this->DisconnectLoginDialog();
}

void MainWindow::on_NotLoggedIn()
{
    this->DisconnectLoginDialog();
}

void MainWindow::on_Canceled()
{
    this->DisconnectLoginDialog();
    this->close();
}

void MainWindow::on_SingleFile()
{
    this->IsSingleFile = true;
    this->DisconnectLoginDialog();
    this->dbl->close();
}

// This is the tab area to go back to the main tab.

void MainWindow::on_BtnMain_clicked()
{
    GoToMainTab();
}

void MainWindow::on_BtnMain_11_clicked()
{
    GoToMainTab();
}

void MainWindow::on_BtnMain_10_clicked()
{
    GoToMainTab();
}

void MainWindow::on_BtnMain_9_clicked()
{
    GoToMainTab();
}

void MainWindow::on_BtnMain_8_clicked()
{
    GoToMainTab();
}

void MainWindow::on_BtnMain_7_clicked()
{
    GoToMainTab();
}

void MainWindow::on_BtnMain_6_clicked()
{
    GoToMainTab();
}

void MainWindow::on_BtnMain_5_clicked()
{
    GoToMainTab();
}

void MainWindow::on_BtnMain_4_clicked()
{
    GoToMainTab();
}

void MainWindow::on_BtnMain_3_clicked()
{
    GoToMainTab();
}

void MainWindow::on_BtnMain_2_clicked()
{
    GoToMainTab();
}

void  MainWindow::GoToMainTab()
{
    ui->tabWidget->setCurrentIndex(0);
}

// This next section is to setup the labels.

void MainWindow::SetupConcertTabs()
{
    ui->statusBar->showMessage("Setting up Concert Tabs", 1000);
    if (ourProtoolsObject->ProductionSize == ProtoolsStandard::SMALL_CONCERT)
    {
        ui->lblSize->setText(tr("Small Concert"));
        ourProtoolsObject->Main_lblSize = tr("Small Concert");
    }
    else if(ourProtoolsObject->ProductionSize == ProtoolsStandard::LARGE_CONCERT)
    {
        ui->lblSize->setText(tr("Large Concert"));
        ourProtoolsObject->Main_lblSize = tr("Large Concert");
    }
    else if(ourProtoolsObject->ProductionSize == ProtoolsStandard::SMALL_FESTIVAL)
    {
        ui->lblSize->setText(tr("Small Festival"));
        ourProtoolsObject->Main_lblSize = tr("Small Festival");
    }
    else if(ourProtoolsObject->ProductionSize == ProtoolsStandard::LARGE_FESTIVAL)
    {
        ui->lblSize->setText(tr("Large Festival"));
        ourProtoolsObject->Main_lblSize = tr("Large Festival");
    }
}

void MainWindow::ClearOutTheScreen()
{
    ui->statusBar->showMessage("Clearing out the screen", 1000);
    ui->lblSize->setText(tr("Small Concert"));
    ourProtoolsObject->Main_lblSize = tr("Small Concert");

    ui->lblAdminCosts->clear();
    ui->lblArtists->clear();
    ui->lblMktgCosts->clear();
    ui->lblNet->clear();
    ui->lblOpsCost->clear();
    ui->lblPerformaceDates->clear();
    ui->lblPossibleDollars->clear();
    ui->lblPossibleTicletCOmm->clear();
    ui->lblProdCost->clear();
    ui->lblSecCosts->clear();
    ui->lblSize->clear();
    ui->lblStaffCost->clear();
    ui->lblSubTotal->clear();
    ui->lblTax->clear();
    ui->lblTicketSalesPos->clear();
    ui->lblVenues->clear();

    // These are the text values.
    ui->_txtAdmin_0->setText(tr("0.00"));
    ui->_txtAdmin_1->setText(tr("0.00"));
    ui->_txtAdmin_2->setText(tr("0.00"));
    ui->_txtAdmin_3->setText(tr("0.00"));
    ui->_txtAdmin_4->setText(tr("0.00"));
    ui->_txtAdmin_5->setText(tr("0.00"));
    ui->_txtAdmin_6->setText(tr("0.00"));
    ui->_txtAdmin_7->setText(tr("0.00"));
    ui->_txtAdmin_8->setText(tr("0.00"));
    ui->_txtAdmin_9->setText(tr("0.00"));
    ui->_txtAdmin_10->setText(tr("0.00"));
    ui->_txtAdmin_11->setText(tr("0.00"));
    ui->_txtAdmin_12->setText(tr("0.00"));
    ui->_txtAdmin_13->setText(tr("0.00"));

    ui->_txtMktg_0->setText(tr("0.00"));
    ui->_txtMktg_1->setText(tr("0.00"));
    ui->_txtMktg_2->setText(tr("0.00"));

    ui->_txtOps_0->setText(tr("0.00"));
    ui->_txtOps_1->setText(tr("0.00"));
    ui->_txtOps_2->setText(tr("0.00"));
    ui->_txtOps_3->setText(tr("0.00"));
    ui->_txtOps_4->setText(tr("0.00"));
    ui->_txtOps_5->setText(tr("0.00"));
    ui->_txtOps_6->setText(tr("0.00"));
    ui->_txtOps_7->setText(tr("0.00"));
    ui->_txtOps_8->setText(tr("0.00"));
    ui->_txtOps_9->setText(tr("0.00"));
    ui->_txtOps_10->setText(tr("0.00"));
    ui->_txtOps_11->setText(tr("0.00"));
    ui->_txtOps_12->setText(tr("0.00"));
    ui->_txtOps_13->setText(tr("0.00"));
    ui->_txtOps_14->setText(tr("0.00"));

    ui->_txtSec_0->setText(tr("0.00"));
    ui->_txtSec_1->setText(tr("0.00"));
    ui->_txtSec_2->setText(tr("0.00"));
    ui->_txtSec_3->setText(tr("0.00"));
    ui->_txtSec_4->setText(tr("0.00"));

    ui->_txtStaff_0->setText(tr("0.00"));
    ui->_txtStaff_1->setText(tr("0.00"));

    ui->_ProdText1_0->setText(tr("0.00"));
    ui->_ProdText1_0->setText(tr("0.00"));
    ui->_ProdText1_2->setText(tr("0.00"));
    ui->_ProdText1_3->setText(tr("0.00"));
    ui->_ProdText1_4->setText(tr("0.00"));
    ui->_ProdText1_5->setText(tr("0.00"));
    ui->_ProdText1_6->setText(tr("0.00"));

    ui->txtVenueName->clear();
    ui->txtVenueAddress1->clear();
    ui->txtVenueAddress2->clear();
    ui->txtVenueAddress3->clear();
    ui->txtVenueCity->clear();
    ui->txtVenueState->clear();
    ui->txtVenueZip->clear();
    ui->txtVenueSpecialNotes->clear();
    ui->lstVenues->clear();

    ui->txtArtistName->clear();
    ui->lstArtists->clear();
    ui->lstPerformaceDates->clear();

    ui->txtName->clear();
    ui->txtCost->clear();
    ui->txtCount->clear();
    ui->txtDescription->clear();
    ui->lstTickets->clear();

    ui->txtTaxName->clear();
    ui->txtRate->clear();
    ui->lstTaxes->clear();

    ui->txtTicketComm->setText(tr("0.00"));
}

void MainWindow::on_actionSmall_Production_triggered()
{
    ourProtoolsObject->ProductionSize = ProtoolsStandard::SMALL_CONCERT;
    SetupConcertTabs();
}

void MainWindow::on_actionLarge_Production_triggered()
{
    ourProtoolsObject->ProductionSize = ProtoolsStandard::LARGE_CONCERT;
    SetupConcertTabs();
}

void MainWindow::on_actionSmall_Festival_triggered()
{
    ourProtoolsObject->ProductionSize = ProtoolsStandard::SMALL_FESTIVAL;
    SetupConcertTabs();
}

void MainWindow::on_actionLarge_Festival_triggered()
{
    ourProtoolsObject->ProductionSize = ProtoolsStandard::LARGE_FESTIVAL;
    SetupConcertTabs();
}

// This is the file menu.

void MainWindow::on_actionE_xit_triggered()
{
    this->close();
}

void MainWindow::on_actionNew_triggered()
{
    delete this->ourProtoolsObject;
    this->ourProtoolsObject = new ProtoolsStandard;
    this->ClearOutTheScreen();
    this->SetupConcertTabs();
    ui->tabWidget->setCurrentIndex(0);
    this->UpdateProtoolsObject();
    this->HasChanged = false;
}

void MainWindow::on_action_Open_triggered()
{
    //Now we are going to open the file.
    QString fileName = QFileDialog::getOpenFileName(this, "Open Data", "", "*.txt", 0, 0);

    // This is going to flush out our old object.
    delete this->ourProtoolsObject;
    this->ourProtoolsObject = new ProtoolsStandard;

    if (!fileName.isNull() && !fileName.isEmpty())
    {
        if (this->ourProtoolsObject->OpenFile(fileName))
        {
            // This is for the admin
            ui->_txtAdmin_0->setText(QString::number(this->ourProtoolsObject->AdminScreen_AdminStaff));
            ui->_txtAdmin_12->setText(QString::number(this->ourProtoolsObject->AdminScreen_ASCAP));
            ui->_txtAdmin_1->setText(QString::number(this->ourProtoolsObject->AdminScreen_BoxOffStaff));
            ui->_txtAdmin_10->setText(QString::number(this->ourProtoolsObject->AdminScreen_Insurance));
            ui->_txtAdmin_8->setText(QString::number(this->ourProtoolsObject->AdminScreen_LegalAccountant));
            ui->_txtAdmin_4->setText(QString::number(this->ourProtoolsObject->AdminScreen_OfficeCosts));
            ui->_txtAdmin_5->setText(QString::number(this->ourProtoolsObject->AdminScreen_OfficeEquipment));
            ui->_txtAdmin_11->setText(QString::number(this->ourProtoolsObject->AdminScreen_Permits));
            ui->_txtAdmin_7->setText(QString::number(this->ourProtoolsObject->AdminScreen_Phones));
            ui->_txtAdmin_6->setText(QString::number(this->ourProtoolsObject->AdminScreen_Postage));
            ui->_txtAdmin_9->setText(QString::number(this->ourProtoolsObject->AdminScreen_RentPower));
            ui->_txtAdmin_13->setText(QString::number(this->ourProtoolsObject->AdminScreen_Ticketprinting));
            ui->_txtAdmin_2->setText(QString::number(this->ourProtoolsObject->AdminScreen_TicketTakers));
            ui->_txtAdmin_3->setText(QString::number(this->ourProtoolsObject->AdminScreen_TrafficControl));

            // This is for the marketing.
            ui->_txtMktg_1->setText(QString::number(this->ourProtoolsObject->MarketingScreen_CreativePrinting));
            ui->_txtMktg_2->setText(QString::number(this->ourProtoolsObject->MarketingScreen_MktgStaff));
            ui->_txtMktg_0->setText(QString::number(this->ourProtoolsObject->MarketingScreen_PublicRelations));

            // This is for the operations.
            ui->_txtOps_6->setText(QString::number(this->ourProtoolsObject->OperationsScreen_EquipmentRent));
            ui->_txtOps_10->setText(QString::number(this->ourProtoolsObject->OperationsScreen_Fence));
            ui->_txtOps_4->setText(QString::number(this->ourProtoolsObject->OperationsScreen_Kamode));
            ui->_txtOps_1->setText(QString::number(this->ourProtoolsObject->OperationsScreen_Laborers));
            ui->_txtOps_14->setText(QString::number(this->ourProtoolsObject->OperationsScreen_LandPrep));
            ui->_txtOps_3->setText(QString::number(this->ourProtoolsObject->OperationsScreen_LandRestore));
            ui->_txtOps_13->setText(QString::number(this->ourProtoolsObject->OperationsScreen_MotorHomes));
            ui->_txtOps_11->setText(QString::number(this->ourProtoolsObject->OperationsScreen_OpsVehicles));
            ui->_txtOps_9->setText(QString::number(this->ourProtoolsObject->OperationsScreen_PerimeterLights));
            ui->_txtOps_7->setText(QString::number(this->ourProtoolsObject->OperationsScreen_Petroleum));
            ui->_txtOps_8->setText(QString::number(this->ourProtoolsObject->OperationsScreen_PowerGenerator));
            ui->_txtOps_12->setText(QString::number(this->ourProtoolsObject->OperationsScreen_ShuttleBuses));
            ui->_txtOps_0->setText(QString::number(this->ourProtoolsObject->OperationsScreen_SiteOpsCoordinator));
            ui->_txtOps_2->setText(QString::number(this->ourProtoolsObject->OperationsScreen_VenueCost));
            ui->_txtOps_5->setText(QString::number(this->ourProtoolsObject->OperationsScreen_WaterTrucks));

            // This is for the production stuff.
            ui->_ProdText1_5->setText(QString::number(this->ourProtoolsObject->ProductionScreen_LightTowers));
            ui->_ProdText1_6->setText(QString::number(this->ourProtoolsObject->ProductionScreen_Aerials));
            ui->_ProdText1_3->setText(QString::number(this->ourProtoolsObject->ProductionScreen_Catering));
            ui->_ProdText1_2->setText(QString::number(this->ourProtoolsObject->ProductionScreen_Lights));
            ui->_ProdText1_1->setText(QString::number(this->ourProtoolsObject->ProductionScreen_Sound));
            ui->_ProdText1_4->setText(QString::number(this->ourProtoolsObject->ProductionScreen_Stage));
            ui->_ProdText1_0->setText(QString::number(this->ourProtoolsObject->ProductionScreen_Talent));

            // This is our security stuff.
            ui->_txtSec_3->setText(QString::number(this->ourProtoolsObject->SecurityScreen_Communications));
            ui->_txtSec_2->setText(QString::number(this->ourProtoolsObject->SecurityScreen_FirstAid));
            ui->_txtSec_4->setText(QString::number(this->ourProtoolsObject->SecurityScreen_Ids));
            ui->_txtSec_0->setText(QString::number(this->ourProtoolsObject->SecurityScreen_SecStaff));
            ui->_txtSec_1->setText(QString::number(this->ourProtoolsObject->SecurityScreen_Vehicles));

            ui->txtTicketComm->setText(QString::number(this->ourProtoolsObject->TicketComm));

            ui->_txtStaff_0->setText(QString::number(this->ourProtoolsObject->StaffScreen_ProdStaff));
            ui->_txtStaff_1->setText(QString::number(this->ourProtoolsObject->StaffScreen_StageHands));

            // This is for the Artists.
            for (int i = 0; i < this->ourProtoolsObject->Artists->count(); i++)
            {
                Artist *a;
                a = this->ourProtoolsObject->Artists->at(i);
                new QListWidgetItem(a->ArtistName, ui->lstArtists);
            }

            // This is for our performance dates
            for(int i = 0; i<this->ourProtoolsObject->PerformanceDates->count(); i++)
            {
                PerformanceDate *p;
                p = this->ourProtoolsObject->PerformanceDates->at(i);
                QString s = tr("");
                s.append(p->PerformanceStartDateTime.toString(Qt::SystemLocaleLongDate));
                s.append("-");
                s.append(p->PerformanceEndDateTime.toString(Qt::SystemLocaleLongDate));
                new QListWidgetItem(s , ui->lstPerformaceDates);
            }

            // This is for the TicketTiers.
            for (int i = 0; i < this->ourProtoolsObject->TicketTiers->count(); i++)
            {
                Ticket *t = this->ourProtoolsObject->TicketTiers->at(i);
                new QListWidgetItem(t->TierName, ui->lstTickets);
            }

            // This is for the Taxes.
            for (int i = 0; i < this->ourProtoolsObject->Taxes->count(); i++)
            {
                Tax *t = this->ourProtoolsObject->Taxes->at(i);
                new QListWidgetItem(t->TaxName, ui->lstTaxes);
            }

            // This is for the Venues.
            for (int i = 0; i < this->ourProtoolsObject->Venues->count(); i++)
            {
                Venue *v = this->ourProtoolsObject->Venues->at(i);
                new QListWidgetItem(v->VenueName, ui->lstVenues);
            }

            this->SetupConcertTabs();
            this->UpdateProtoolsObject();
        }
    }

    this->SetCurrentFile(fileName);
}

void MainWindow::on_action_Save_triggered()
{
    df = new DataBaseFacade;
    ComResponse *c = new ComResponse;
    //df->InsertProduction(this->ourProtoolsObject, c);
}

void MainWindow::on_actionS_ave_As_triggered()
{
    fileString.clear();

    if (this->SaveFile())
    {
        this->ourProtoolsObject->WriteFile(fileString);
    }
}

bool MainWindow::SaveFile()
{
    if(fileString.isNull() || fileString.isEmpty())
    {
        QString file = QFileDialog::getSaveFileName(this, "Save Data", "","*.txt", 0, 0);

        if (file.isNull() || file.isEmpty())
        {
            return false;
        }
        else
        {
            this->SetCurrentFile(file);
        }
    }

    return true;
}

void MainWindow::SetCurrentFile(const QString &fileName)
{
    fileString = fileName;
}

void MainWindow::on_action_About_triggered()
{
    QMessageBox::about(this, tr("About Production Estimation Tool"),
    tr("<h2>Production Estimation Tool 1.1</h2>"
    "<p>This was an application I wrote several years back and</p>"
    "<p>I thought it was a good idea to port it to Qt and C++.</p>"
    "<p>This is just something to help promoters and production professionals estimate costs.</p>"
    "<p>You can access the source code and other documentation <a href='http://entertools.codeplex.com'>here.</a></p>"));
}

// This is the action from the main screen buttons

void MainWindow::on_btnVenues_clicked()
{
    ui->tabWidget->setCurrentIndex(1);
}

void MainWindow::on_btnArtists_clicked()
{
    ui->tabWidget->setCurrentIndex(2);
}

void MainWindow::on_btnSetDate_clicked()
{
    ui->tabWidget->setCurrentIndex(3);
}

void MainWindow::on_btnProd_clicked()
{
    ui->tabWidget->setCurrentIndex(4);
}

void MainWindow::on_btnProductionStaff_clicked()
{
    ui->tabWidget->setCurrentIndex(5);
}

void MainWindow::on_btnAdminCosts_clicked()
{
    ui->tabWidget->setCurrentIndex(6);
}

void MainWindow::on_btnMarketingCosts_clicked()
{
    ui->tabWidget->setCurrentIndex(7);
}

void MainWindow::on_btnSecurityCosts_clicked()
{
    ui->tabWidget->setCurrentIndex(8);
}

void MainWindow::on_btnOperationsCosts_clicked()
{
    ui->tabWidget->setCurrentIndex(9);
}

void MainWindow::on_btnEditTickets_clicked()
{
    ui->tabWidget->setCurrentIndex(10);
}

void MainWindow::on_btnTaxRates_clicked()
{
    ui->tabWidget->setCurrentIndex(11);
}

// This is to update the object from the screen input
void MainWindow::UpdateProtoolsObject()
{
    ui->statusBar->showMessage("Updating the information", 1000);
    // this is setting some stuff off our object.
    this->ourProtoolsObject->StaffScreen_ProdStaff = ui->_txtStaff_0->text().toDouble();
    this->ourProtoolsObject->StaffScreen_StageHands = ui->_txtStaff_1->text().toDouble();

    this->ourProtoolsObject->SecurityScreen_SecStaff = ui->_txtSec_0->text().toDouble();
    this->ourProtoolsObject->SecurityScreen_Vehicles = ui->_txtSec_1->text().toDouble();
    this->ourProtoolsObject->SecurityScreen_FirstAid = ui->_txtSec_2->text().toDouble();
    this->ourProtoolsObject->SecurityScreen_Communications = ui->_txtSec_3->text().toDouble();
    this->ourProtoolsObject->SecurityScreen_Ids = ui->_txtSec_4->text().toDouble();

    this->ourProtoolsObject->ProductionScreen_Talent = ui->_ProdText1_0->text().toDouble();
    this->ourProtoolsObject->ProductionScreen_Sound = ui->_ProdText1_1->text().toDouble();
    this->ourProtoolsObject->ProductionScreen_Lights = ui->_ProdText1_2->text().toDouble();
    this->ourProtoolsObject->ProductionScreen_Catering = ui->_ProdText1_3->text().toDouble();
    this->ourProtoolsObject->ProductionScreen_Stage = ui->_ProdText1_4->text().toDouble();
    this->ourProtoolsObject->ProductionScreen_LightTowers = ui->_ProdText1_5->text().toDouble();
    this->ourProtoolsObject->ProductionScreen_Aerials = ui->_ProdText1_6->text().toDouble();

    this->ourProtoolsObject->OperationsScreen_SiteOpsCoordinator = ui->_txtOps_0->text().toDouble();
    this->ourProtoolsObject->OperationsScreen_Laborers = ui->_txtOps_1->text().toDouble();
    this->ourProtoolsObject->OperationsScreen_VenueCost = ui->_txtOps_2->text().toDouble();
    this->ourProtoolsObject->OperationsScreen_LandRestore = ui->_txtOps_3->text().toDouble();
    this->ourProtoolsObject->OperationsScreen_Kamode = ui->_txtOps_4->text().toDouble();
    this->ourProtoolsObject->OperationsScreen_WaterTrucks = ui->_txtOps_5->text().toDouble();
    this->ourProtoolsObject->OperationsScreen_EquipmentRent = ui->_txtOps_6->text().toDouble();
    this->ourProtoolsObject->OperationsScreen_Petroleum = ui->_txtOps_7->text().toDouble();
    this->ourProtoolsObject->OperationsScreen_PowerGenerator = ui->_txtOps_8->text().toDouble();
    this->ourProtoolsObject->OperationsScreen_PerimeterLights = ui->_txtOps_9->text().toDouble();
    this->ourProtoolsObject->OperationsScreen_Fence = ui->_txtOps_10->text().toDouble();
    this->ourProtoolsObject->OperationsScreen_OpsVehicles = ui->_txtOps_11->text().toDouble();
    this->ourProtoolsObject->OperationsScreen_ShuttleBuses = ui->_txtOps_12->text().toDouble();
    this->ourProtoolsObject->OperationsScreen_MotorHomes = ui->_txtOps_13->text().toDouble();
    this->ourProtoolsObject->OperationsScreen_LandPrep = ui->_txtOps_14->text().toDouble();

    this->ourProtoolsObject->MarketingScreen_PublicRelations = ui->_txtMktg_0->text().toDouble();
    this->ourProtoolsObject->MarketingScreen_CreativePrinting = ui->_txtMktg_1->text().toDouble();
    this->ourProtoolsObject->MarketingScreen_MktgStaff = ui->_txtMktg_2->text().toDouble();

    this->ourProtoolsObject->AdminScreen_AdminStaff = ui->_txtAdmin_0->text().toDouble();
    this->ourProtoolsObject->AdminScreen_BoxOffStaff = ui->_txtAdmin_1->text().toDouble();
    this->ourProtoolsObject->AdminScreen_TicketTakers = ui->_txtAdmin_2->text().toDouble();
    this->ourProtoolsObject->AdminScreen_TrafficControl = ui->_txtAdmin_3->text().toDouble();
    this->ourProtoolsObject->AdminScreen_OfficeCosts = ui->_txtAdmin_4->text().toDouble();
    this->ourProtoolsObject->AdminScreen_OfficeEquipment = ui->_txtAdmin_5->text().toDouble();
    this->ourProtoolsObject->AdminScreen_Postage = ui->_txtAdmin_6->text().toDouble();
    this->ourProtoolsObject->AdminScreen_Phones = ui->_txtAdmin_7->text().toDouble();
    this->ourProtoolsObject->AdminScreen_LegalAccountant = ui->_txtAdmin_8->text().toDouble();
    this->ourProtoolsObject->AdminScreen_RentPower = ui->_txtAdmin_9->text().toDouble();
    this->ourProtoolsObject->AdminScreen_Insurance =ui->_txtAdmin_10->text().toDouble();
    this->ourProtoolsObject->AdminScreen_Permits = ui->_txtAdmin_11->text().toDouble();
    this->ourProtoolsObject->AdminScreen_ASCAP = ui->_txtAdmin_12->text().toDouble();
    this->ourProtoolsObject->AdminScreen_Ticketprinting = ui->_txtAdmin_13->text().toDouble();

    this->ourProtoolsObject->ProductionName = ui->txtProductionName->text();

    this->ourProtoolsObject->RecalculateProtoolsObject();

    ui->lblSize->setText(ourProtoolsObject->Main_lblSize);
    ui->lblProdCost->setText(this->ourProtoolsObject->Main_lblProdCost);
    ui->lblStaffCost->setText(this->ourProtoolsObject->Main_lblStaffCost);
    ui->lblAdminCosts->setText(this->ourProtoolsObject->Main_lblAdminCosts);
    ui->lblMktgCosts->setText(this->ourProtoolsObject->Main_lblMktgCosts);
    ui->lblSecCosts->setText(this->ourProtoolsObject->Main_lblSecCosts);
    ui->lblOpsCost->setText(this->ourProtoolsObject->Main_lblOpsCost);
    ui->lblSubTotal->setText(this->ourProtoolsObject->Main_lblSubTotal);
    ui->lblArtists->setText(this->ourProtoolsObject->Main_lblArtists);
    ui->lblVenues->setText(this->ourProtoolsObject->Main_lblVenues);
    ui->lblPerformaceDates->setText(this->ourProtoolsObject->Main_lblPerformaceDates);
    ui->lblTicketSalesPos->setText(this->ourProtoolsObject->Main_TicketCount);
    ui->lblPossibleDollars->setText(this->ourProtoolsObject->Main_lblTicketSalesPos);
    ui->lblTax->setText(this->ourProtoolsObject->Main_lblTax);
    ui->lblPossibleTicletCOmm->setText(this->ourProtoolsObject->Main_lblPossibleTicletCOmm);
    ui->lblNet->setText(this->ourProtoolsObject->Main_lblNet);
    this->HasChanged = true;
}

// This is for the venues screen.

void MainWindow::on_btnAddVenue_clicked()
{
    // This is inserting the text into the venue.
    new QListWidgetItem(ui->txtVenueName->text(), ui->lstVenues);

    // This is going to put the venue into the venues list.
    this->ourProtoolsObject->AddVenue(
                ui->txtVenueName->text(),
                ui->txtVenueAddress1->text(),
                ui->txtVenueAddress2->text(),
                ui->txtVenueAddress3->text(),
                ui->txtVenueCity->text(),
                ui->txtVenueState->text(),
                ui->txtVenueZip->text(),
                ui->txtVenueSpecialNotes->toPlainText());

    ui->txtVenueName->clear();
    ui->txtVenueAddress1->clear();
    ui->txtVenueAddress2->clear();
    ui->txtVenueAddress3->clear();
    ui->txtVenueCity->clear();
    ui->txtVenueState->clear();
    ui->txtVenueZip->clear();
    ui->txtVenueSpecialNotes->clear();

    this->UpdateProtoolsObject();
}

void MainWindow::on_btnRemoveVenue_clicked()
{
    if (ui->lstVenues->count() > 0)
    {
        this->IsDeletingVenues = true;
        int i = ui->lstVenues->currentRow();
        this->ourProtoolsObject->RemoveVenue(i);
        QListWidgetItem *q = ui->lstVenues->currentItem();
        ui->lstVenues->removeItemWidget(q);
        delete q;
        ui->txtOutVenues->clear();
        this->UpdateProtoolsObject();
        this->IsDeletingVenues = false;

        if (ui->lstVenues->count() == 1)
        {
            ui->txtOutVenues->setText(this->ourProtoolsObject->GetVenueText(0));
        }
    }
}

// This is the artists screen.

void MainWindow::on_btnAddArtist_clicked()
{
    // This is going to insert the widget into the artists list widget.
    new QListWidgetItem(ui->txtArtistName->text(), ui->lstArtists);

    // This is going to insert the item in the protools list.
    this->ourProtoolsObject->AddArtist(ui->txtArtistName->text());

    ui->txtArtistName->clear();

    this->UpdateProtoolsObject();
}

void MainWindow::on_btnRemoveArtist_clicked()
{
    if (ui->lstArtists->count() > 0)
    {
        this->IsDeletingArtists = true;
        int i = ui->lstArtists->currentRow();
        this->ourProtoolsObject->RemoveArtist(i);
        QListWidgetItem *q = ui->lstArtists->currentItem();
        ui->lstArtists->removeItemWidget(q);
        delete q;
        ui->txtOutArtists->clear();
        this->UpdateProtoolsObject();
        this->IsDeletingArtists = false;

        if (ui->lstArtists->count() == 1)
        {
            ui->txtOutArtists->setText(this->ourProtoolsObject->GetArtistText(0));
        }
    }
}

// This is for the dates screen

void MainWindow::on_btnAddDate_clicked()
{
    // Adding the item to the date list...
    QString s = tr("");
    s.append(ui->txtStartDate->dateTime().toString(Qt::SystemLocaleLongDate));
    s.append("-");
    s.append(ui->txtEndDate->dateTime().toString(Qt::SystemLocaleLongDate));

    new QListWidgetItem(s, ui->lstPerformaceDates);

    // This is going to add the dates to the protools list.
    this->ourProtoolsObject->AddPerformanceDate(ui->txtStartDate->dateTime(), ui->txtEndDate->dateTime());
    this->UpdateProtoolsObject();
}

void MainWindow::on_btnRemoveDate_clicked()
{
    if(ui->lstPerformaceDates->count() > 0)
    {
        this->IsDeletingDates = true;
        int i = ui->lstPerformaceDates->currentRow();
        this->ourProtoolsObject->RemovePerformanceDate(i);
        QListWidgetItem *q = ui->lstPerformaceDates->currentItem();
        ui->lstPerformaceDates->removeItemWidget(q);
        delete q;
        ui->txtOutDates->clear();
        this->UpdateProtoolsObject();
        this->IsDeletingDates = false;

        if (ui->lstPerformaceDates->count() == 1)
        {
            ui->txtOutDates->setText(this->ourProtoolsObject->GetPerformanceDateText(0));
        }
    }
}

// This is for the production costs

void MainWindow::on__ProdText1_0_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__ProdText1_1_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__ProdText1_2_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__ProdText1_3_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__ProdText1_4_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__ProdText1_5_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__ProdText1_6_lostFocus()
{
    this->UpdateProtoolsObject();
}

// This is for the staff costs

void MainWindow::on__txtStaff_0_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtStaff_1_lostFocus()
{
    this->UpdateProtoolsObject();
}

// This is for the admin costs

void MainWindow::on__txtAdmin_0_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtAdmin_1_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtAdmin_2_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtAdmin_3_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtAdmin_4_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtAdmin_5_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtAdmin_6_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtAdmin_7_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtAdmin_8_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtAdmin_9_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtAdmin_10_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtAdmin_11_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtAdmin_12_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtAdmin_13_lostFocus()
{
    this->UpdateProtoolsObject();
}

// This is for the marketing costs

void MainWindow::on__txtMktg_0_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtMktg_1_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtMktg_2_lostFocus()
{
    this->UpdateProtoolsObject();
}

// This is for the security costs

void MainWindow::on__txtSec_0_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtSec_1_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtSec_2_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtSec_3_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtSec_4_lostFocus()
{
    this->UpdateProtoolsObject();
}

// This is for the operations costs

void MainWindow::on__txtOps_0_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtOps_1_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtOps_2_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtOps_3_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtOps_4_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtOps_5_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtOps_6_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtOps_7_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtOps_8_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtOps_9_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtOps_10_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtOps_11_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtOps_12_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtOps_13_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on__txtOps_14_lostFocus()
{
    this->UpdateProtoolsObject();
}

// This is the ticket information

void MainWindow::on_btnAddTicket_clicked()
{
    // This is going to insert the widget into the artists list widget.
    new QListWidgetItem(ui->txtName->text(), ui->lstTickets);

    // This is going to insert the item in the protools list.
    this->ourProtoolsObject->AddTicket(ui->txtName->text(), ui->txtCost->text().toDouble(), ui->txtDescription->text(), ui->txtCount->text().toInt());

    ui->txtName->clear();
    ui->txtCost->clear();
    ui->txtCount->clear();
    ui->txtDescription->clear();

    this->UpdateProtoolsObject();

    ui->lstTickets->setCurrentRow(0);
}

void MainWindow::on_btnRemoveTicket_clicked()
{
    if (ui->lstTickets->count() > 0)
    {
        this->IsDeletingTickets = true;
        int i = ui->lstTickets->currentRow();
        this->ourProtoolsObject->RemoveTicket(i);
        QListWidgetItem *q = ui->lstTickets->currentItem();
        ui->lstTickets->removeItemWidget(q);
        delete q;
        ui->txtOutTickets->clear();
        this->UpdateProtoolsObject();
        this->IsDeletingTickets = false;

        if (ui->lstTickets->count() == 1)
        {
            ui->txtOutTickets->setText(this->ourProtoolsObject->GetTicketText(0));
        }
    }
}

// This is the tax information

void MainWindow::on_btnAddTax_clicked()
{
    // This is going to insert the widget into the artists list widget.
    new QListWidgetItem(ui->txtTaxName->text(), ui->lstTaxes);

    // This is going to insert the item in the protools list.
    this->ourProtoolsObject->AddTax(ui->txtTaxName->text(), ui->txtRate->text().toDouble());

    ui->txtTaxName->clear();
    ui->txtRate->clear();

    this->UpdateProtoolsObject();
}

void MainWindow::on_btnRemoveTax_clicked()
{
    if (ui->lstTaxes->count() > 0)
    {
        this->IsDeletingTaxes = true;
        int i = ui->lstTaxes->currentRow();
        this->ourProtoolsObject->RemoveTax(i);
        QListWidgetItem *q = ui->lstTaxes->currentItem();
        ui->lstTaxes->removeItemWidget(q);
        delete q;
        ui->txtOutTaxes->clear();
        this->UpdateProtoolsObject();
        this->IsDeletingTaxes = false;

        if (ui->lstTaxes->count() == 1)
        {
            ui->txtOutTaxes->setText(this->ourProtoolsObject->GetTaxText(0));
        }
    }
}

void MainWindow::on_txtTicketComm_lostFocus()
{
    this->UpdateProtoolsObject();
}

void MainWindow::on_lstTickets_currentRowChanged(int currentRow)
{
    if (!this->IsDeletingTickets)
    {
        ui->txtOutTickets->setText(this->ourProtoolsObject->GetTicketText(currentRow));
    }
}

void MainWindow::on_lstVenues_currentRowChanged(int currentRow)
{
    if (!this->IsDeletingVenues)
    {
        ui->txtOutVenues->setText(this->ourProtoolsObject->GetVenueText(currentRow));
    }
}

void MainWindow::on_lstArtists_currentRowChanged(int currentRow)
{
    if(!this->IsDeletingArtists)
    {
        ui->txtOutArtists->setText(this->ourProtoolsObject->GetArtistText(currentRow));
    }
}

void MainWindow::on_lstPerformaceDates_currentRowChanged(int currentRow)
{
    if (!this->IsDeletingDates)
    {
        ui->txtOutDates->setText(this->ourProtoolsObject->GetPerformanceDateText(currentRow));
    }
}

void MainWindow::on_lstTaxes_currentRowChanged(int currentRow)
{
    if (!this->IsDeletingTaxes)
    {
        ui->txtOutTaxes->setText(this->ourProtoolsObject->GetTaxText(currentRow));
    }
}

void MainWindow::on_actionPlain_Text_triggered()
{
    ui->statusBar->showMessage("Exporting Text");
    QString file = QFileDialog::getSaveFileName(this, "Save Text File", "", "*.txt", 0, 0);

    if (file.isNull() || file.isEmpty())
    {
        return;
    }

    if (!this->ourProtoolsObject->ExportToText(file)) { // failed to open file
        QMessageBox::warning(this, "Warning", "failed to open file, is it writable?", QMessageBox::Ok);
        return;
    }

    ui->statusBar->showMessage("Export Complete", 500);
}

void MainWindow::on_actionHTML_Report_triggered()
{
    ui->statusBar->showMessage("Exporting HTML");
    QString file = QFileDialog::getSaveFileName(this, "Save HTML", "", "*.html", 0, 0);

    if (file.isNull() || file.isEmpty())
    {
        return;
    }

    if (!this->ourProtoolsObject->ExportToHTML(file)) { // failed to open file
        QMessageBox::warning(this, "Warning", "failed to open file, is it writable?", QMessageBox::Ok);
        return;
    }

    ui->statusBar->showMessage("Export Complete", 500);
}

void MainWindow::on_actionPDF_triggered()
{
    ui->statusBar->showMessage("Exporting PDF");
    QString file = QFileDialog::getSaveFileName(this, "Save PDF", "", "*.pdf", 0, 0);

    if (file.isNull() || file.isEmpty())
    {
        return;
    }

    if (!this->ourProtoolsObject->ExportToPDF(file)) { // failed to open file
        QMessageBox::warning(this, "Warning", "failed to open file, is it writable?", QMessageBox::Ok);
        return;
    }

    ui->statusBar->showMessage("Export Complete", 500);
}

void MainWindow::on_actionLicense_triggered()
{
    ld = new LicenseDialog;
    connect(ld, SIGNAL(SelectedOK()), this, SLOT(on_license_selected_ok()));
    ld->exec();
}

void MainWindow::on_license_selected_ok()
{
    ld->close();
    disconnect(ld, SIGNAL(SelectedOK()), this, SLOT(on_license_selected_ok()));
    delete ld;
}

void MainWindow::on_actionDatabase_Settings_triggered()
{
    sd = new SettingsDialog;
    connect(sd, SIGNAL(DatabaseSettingsUpdated()), this, SLOT(on_DatabaseSettingsUpdated()));
    connect(sd, SIGNAL(DatabaseSettingsAborted()), this, SLOT(on_DatabaseSettingsAborted()));
    sd->exec();
}

void MainWindow::on_DatabaseSettingsUpdated()
{
    sd->close();
    disconnect(sd, SIGNAL(DatabaseSettingsUpdated()), this, SLOT(on_DatabaseSettingsUpdated()));
    disconnect(sd, SIGNAL(DatabaseSettingsAborted()), this, SLOT(on_DatabaseSettingsAborted()));
    delete sd;
}

void MainWindow::on_DatabaseSettingsAborted()
{
    sd->close();
    disconnect(sd, SIGNAL(DatabaseSettingsUpdated()), this, SLOT(on_DatabaseSettingsUpdated()));
    disconnect(sd, SIGNAL(DatabaseSettingsAborted()), this, SLOT(on_DatabaseSettingsAborted()));
    delete sd;
}

void MainWindow::on_txtProductionName_lostFocus()
{
    this->UpdateProtoolsObject();
}
